<?php
declare (strict_types=1);

namespace app\model\admin;

use app\common\model\BaseModel;
use \libs\Log;

class Config extends BaseModel
{
    protected $pk = 'id';
    protected $json = ['config_value'];

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @param $data
     * @return array
     * 获取配置
     */
    public function getOne($data)
    {
        $configValue = getConfig($data['config_key']);
        return $this->returnRight($configValue);
    }

    /**
     * 更新
     * @param array $data
     * @return array
     */
    public function edit(array $data)
    {
        $configValue = getConfig($data['config_key']);
        if (empty($configValue)) {
            $res = self::create($data);
        } else {
            $res = self::update(['config_value' => $data['config_value']], ['config_key' => $data['config_key']]);
        }
        if (!empty($res)) {
            return $this->returnSucc('编辑成功');
        } else {
            return $this->returnError(1);
        }
    }
}
